package com.caltco.cargo.ops.modules.sys.controller;

import com.caltco.cargo.ops.common.api.R2;
import com.caltco.cargo.ops.common.api.Result;
import com.caltco.cargo.ops.modules.sys.request.TaskRequest;
import com.caltco.cargo.ops.modules.sys.service.TaskService;
import com.caltco.cargo.ops.modules.sys.vo.ProjectVO;
import com.caltco.cargo.ops.modules.sys.vo.TaskGanttVO;
import com.caltco.cargo.ops.modules.sys.vo.TaskVO;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Slf4j
@RestController("taskController")
@RequestMapping("/api/task")
public class TaskController {
    private final TaskService taskService;

    public TaskController(TaskService taskService) {
        this.taskService = taskService;
    }


    @PostMapping("save")
    @Operation(summary = "新增任务")
    public Result<String> save(@RequestPart("taskRequest") TaskRequest taskRequest, // 必须用 @RequestPart
                               @RequestParam(value = "files", required = false) MultipartFile[] files,
                               @RequestParam(value = "taskId", required = false) Long taskId) {
        return taskService.save(taskRequest, files, taskId) ? R2.setOkMsg("新增成功"):R2.setErrMsg(("新增失败"));
    }

    @PutMapping("update")
    @Operation(summary = "修改任务")
    public Result<String> update(@RequestPart("taskRequest") TaskRequest taskRequest,@RequestParam(value = "files", required = false) MultipartFile[] files, @RequestParam(value = "taskId", required = false) Long taskId, @RequestParam(value = "fileIds", required = false) List<String> fileIds) {
        return taskService.update(taskRequest, files, taskId, fileIds) ? R2.setOkMsg("修改成功"):R2.setErrMsg(("修改失败"));
    }

    @PostMapping("get")
    @Operation(summary = "查找任务")
    public Result<List<TaskVO>> get(@RequestBody TaskRequest taskRequest) {
        return R2.ok(taskService.get(taskRequest));
    }

    @PostMapping("getGantt")
    @Operation(summary = "查找甘特图任务")
    public Result<List<TaskGanttVO>> getGantt(@RequestBody TaskRequest taskRequest) {
        return R2.ok(taskService.getGantt(taskRequest));
    }

    @GetMapping("getGanttProject")
    @Operation(summary = "查找甘特图项目任务")
    public Result<List<ProjectVO>> getGanttProject(@RequestParam(required = false) String taskName, @RequestParam(required = false) Long projectId) {
        return R2.ok(taskService.getGanttProject(taskName, projectId));
    }

    @GetMapping("getMessage/{id}")
    @Operation(summary = "查找任务")
    public Result<TaskVO> getMassage(@PathVariable Long id) {
        return R2.ok(taskService.getMassage(id));
    }



    @DeleteMapping("remove/{id}")
    @Operation(summary = "删除任务")
    public Result<String> remove(@PathVariable("id") Long id) {
        return taskService.remove(id) ? R2.setOkMsg("删除成功"):R2.setErrMsg(("删除失败"));
    }
}
